import { deleteAction, getAction, postAction, putAction } from '@/api/manage'
import Vue from 'vue'
import { UI_CACHE_DB_DICT_DATA } from '@/store/mutation-types'

// 重复校验
const duplicateCheck = (params) => getAction("/sys/duplicate/check", params);

// 用户
const selectUserPage = (params) => getAction("/sys/user/page", params);
const selectUserList = (params) => getAction("/sys/user/list", params);
const getUserById = (params) => getAction(`/sys/user/${ params }`);
const queryPermissionsByUser = () => getAction("/sys/user/getUserPermissionByToken");
//改变密码
const changePassword = (params) => putAction("/sys/user/changePassword", params);

// 部门
const searchByKeywords = (params) => getAction("/sys/dept/list", params);
const queryDepartTreeList = (params) => getAction("/sys/dept/treeList", params);
const addDepart = (params) => postAction("/sys/dept", params);
const updDepart = (params) => putAction("/sys/dept", params);
const delDepart = (params) => deleteAction(`/sys/dept/${ params }`);

// 角色
const queryRoleById = (params) => getAction(`/sys/role/${ params }`);
const addRole = (params) => postAction("/sys/role", params);
const updRole = (params) => putAction("/sys/role", params);
const delRole = (params) => deleteAction("/sys/role", params);

// 菜单
const queryMenuTreeList = (params) => getAction("/sys/menu/list", params);
const queryMenuById = (params) => getAction(`/sys/menu/${ params }`);

//从缓存中获取字典配置
function getDictItemsFromCache (dictCode) {
  if (Vue.ls.get(UI_CACHE_DB_DICT_DATA) && Vue.ls.get(UI_CACHE_DB_DICT_DATA)[dictCode]) {
    let dictItems = Vue.ls.get(UI_CACHE_DB_DICT_DATA)[dictCode];
    return dictItems;
  }
}

export {
  duplicateCheck,
  selectUserPage,
  selectUserList,
  getUserById,
  queryPermissionsByUser,
  changePassword,
  searchByKeywords,
  queryDepartTreeList,
  addDepart,
  updDepart,
  delDepart,
  queryRoleById,
  queryMenuTreeList,
  queryMenuById,
  getDictItemsFromCache,
}
